package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class QuickSort {

	public static void main(String[] args) {
		int[] arr = {-9,78,0,23,-567,70, -1,900, 4561};
		
		System.out.println(Arrays.toString(arr));
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("开始时间=" + date1Str);
		
		quickSort(arr, 0, arr.length-1);
		
		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("结束时间=" + date2Str);
	
	}

	public static void quickSort(int[] arr,int left, int right) {
		int l = left; 
		int r = right; 
		
		int pivot = arr[(left + right) / 2];
		int temp = 0; 
	
		while( l < r) { 
			
			
			while( arr[l] < pivot) {
				l += 1;
			}
			
			while(arr[r] > pivot) {
				r -= 1;
			}
			
			if( l >= r) {
				break;
			}
			
			System.out.println("ss");
			System.out.println(l);
		    System.out.println(r);
			temp = arr[l];
			arr[l] = arr[r];
			arr[r] = temp;
			
		
			if(arr[l] == pivot) {
				r -= 1;
			}
		
			if(arr[r] == pivot) {
				l += 1;
			}
			System.out.println("tt");
			System.out.println(l);
		    System.out.println(r);
		    System.out.println(Arrays.toString(arr));
		}
		
		
		if (l == r) {
			l += 1;
			r -= 1;
		}
		
		if(left < r) {
			quickSort(arr, left, r);
		}
	
		if(right > l) {
			quickSort(arr, l, right);
		}
		
		
		
	}
}
